package com.ycMc.service.impl;

import com.ycMc.bean.request.InStockRequest;
import com.ycMc.common.bean.PageBean;
import com.ycMc.domain.InStock;
import com.ycMc.domain.Product;
import com.ycMc.mapper.InStockMapper;
import com.ycMc.mapper.ProductMapper;
import com.ycMc.service.BaseService;
import com.ycMc.service.InStockService;
import org.omg.PortableServer.REQUEST_PROCESSING_POLICY_ID;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by libo2 on 2017/12/12.
 */
@Service
public class InStockServiceImpl  extends BaseService implements InStockService {

    @Autowired
    private InStockMapper inStockMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public void delete(int id) {
        InStock inStock1 = inStockMapper.selectOne(id);

        //还原库存
        productMapper.updateSubtractionStore(inStock1.getProductId(),inStock1.getAddNumber()*1);


        inStockMapper.delete(id);

    }

    @Override
    public void insert(InStockRequest request) {

        InStock inStock = new InStock();


        BeanUtils.copyProperties(request,inStock);
        inStock.setCreateTime(System.currentTimeMillis());
        inStock.setId(0);


        if(null!=inStock.getProductId())
        {
            Product product = productMapper.selectOne(inStock.getProductId());
            inStock.setOriginalNumber(product.getStore());
            inStock.setPresentNumber(request.getAddNumber()+inStock.getOriginalNumber());
            inStock.setProductBrand(product.getProductBrand());
            inStock.setProductName(product.getProductName());
            inStock.setProductVersion(product.getProductVersion());
        }

        productMapper.updateSubtractionStore(request.getProductId(), request.getAddNumber() * -1);

        inStockMapper.insert(inStock);

    }

    @Override
    public void update(InStockRequest request) {
        InStock inStock = new InStock();

        InStock inStock1 = inStockMapper.selectOne(request.getId());

        BeanUtils.copyProperties(request,inStock);

        //如果前端传递的值与之前相等，或者 等于0 则不修改 产品库存
        if(request.getAddNumber()==0||request.getAddNumber()==inStock1.getAddNumber()){

        }
        else{
            productMapper.updateSubtractionStore(inStock1.getProductId(),inStock1.getAddNumber()*1);
            productMapper.updateSubtractionStore(inStock1.getProductId(),request.getAddNumber()*-1);
        }


        inStockMapper.update(inStock);
    }

    @Override
    public PageBean select(InStockRequest request) {

        final int _currentPage = PageBean.countCurrentPage(request.getCurrentPage());
        final int offset = PageBean.countOffset(request.getPageSize(),request.getCurrentPage());

        List<InStock>  inStocks = inStockMapper.select(request.getProductId(),request.getProductName(),request.getProductVersion(),request.getAddNumber(),request.getInStockPrice(),request.getSupplier(),request.getProductBrand(),request.getStartTime(),
                request.getEndTime(),offset, request.getPageSize());

        return createPageBean(_currentPage,inStocks,request.getPageSize());
    }

    @Override
    public PageBean query(InStockRequest request) {

        final int _currentPage = PageBean.countCurrentPage(request.getCurrentPage());
        final int offset = PageBean.countOffset(request.getPageSize(),request.getCurrentPage());

        List<InStock>  inStocks = inStockMapper.query(request.getQueryKeyword(),request.getProductBrand(),request.getStartTime(),request.getEndTime(),offset,request.getPageSize(),request.getProductId());

        return createPageBean(_currentPage,inStocks,request.getPageSize());
    }


    @Override
    public InStock selectOne(int id) {

        return inStockMapper.selectOne(id);
    }
}
